package com.dongjiantong.web.boss.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.AreaWebModule;
import com.dongjiantong.sys.model.WebModule;
import com.dongjiantong.sys.service.AreaWebModuleService;
import com.dongjiantong.sys.service.WebModuleService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.shaded.com.google.common.collect.Maps;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * Created by ldf on 2018/1/16.
 */
@OpenBean
@Slf4j
public class WebModuleController {
    @Reference(version = "1.0.0")
    private WebModuleService webModuleService;
    @Reference(version = "1.0.0")
    private AreaWebModuleService areaWebModuleService;

    /**
     * 查询所有的WebModule
     *
     * @return
     */
    @OpenMethod(key = "boss.WebModule.selectAll",

            httpMethods = RequestMethod.GET)
    public Response<List<Object>> selectAll() {

        return Response.ok(treeNode(0L));
    }

    private List<Object> treeNode(long pid) {

        List<WebModule> childTreeNodes = webModuleService.selectByPid(pid);

        List<Object> node = null;

        if (childTreeNodes.size() > 0) {
            node = Lists.newArrayList();

            for (WebModule webModule : childTreeNodes) {

                if (webModule.getPid() == pid) {
                    Map<String, Object> childArray = Maps.newHashMap();
                    childArray.put("label", webModule.getName());
                    childArray.put("value", webModule.getNumber());
                    childArray.put("level", webModule.getLevel());
                    List<Object> objectList = treeNode(webModule.getId());
                    if (objectList != null) {
                        if (objectList.size() != 0) {
                            childArray.put("children", objectList);
                        }

                    }
                    node.add(childArray);

                }

            }
        }
        return node;


    }

    /**
     * 创建web页面模块
     *
     * @param webModule
     * @return
     */
    @OpenMethod(key = "boss.WebModule.createWebModule",
            paramNames = {"WebModule"},
            httpMethods = RequestMethod.POST)
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class, RuntimeException.class})
    public Response<String> createWebModule(WebModule webModule) {
        try {
            webModuleService.insert(webModule);
            return Response.ok("增加模块成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail(HttpCode.DATA_CREATE_FAIL, "增加模块失败");
        }

    }

    /**
     * 创建设置web平台所显示模块
     *
     * @param mapList
     * @return
     */
    @OpenMethod(key = "boss.AreaWebModule.createAreaWebModule",
            paramNames = {"mapList", "regionAccountId"},
            httpMethods = RequestMethod.POST)
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class, RuntimeException.class})
    public Response<String> createAreaWebModule(List<Map> mapList, Long regionAccountId) {
        List<AreaWebModule> areaWebModuleList = new ArrayList<>();
        for (Map map : mapList) {
            AreaWebModule areaWebModule = new AreaWebModule();
            areaWebModule.setRegionAccountId(Long.parseLong(map.get("regionAccountId").toString()));
            areaWebModule.setWebModuleName((String) map.get("webModuleName"));
            areaWebModule.setWebModuleNumber((String) map.get("webModuleNumber"));
            areaWebModuleList.add(areaWebModule);
        }
        try {
            AreaWebModule areaWebModule = new AreaWebModule();
            areaWebModule.setRegionAccountId(regionAccountId);
            areaWebModuleService.delete(areaWebModule);
            areaWebModuleService.insertList(areaWebModuleList);
            return Response.ok("增加模块成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail(HttpCode.DATA_CREATE_FAIL, "增加模块失败");
        }
    }

    /**
     * 查询开户地区所显示模块code
     *
     * @param regionAccountId
     * @return
     */
    @OpenMethod(key = "boss.AreaWebModule.selectWebModuleCode",
            paramNames = {"regionAccountId"},
            httpMethods = RequestMethod.GET)
    public Response<List> selectWebModuleCode(Long regionAccountId) {
        AreaWebModule areaWebModule = new AreaWebModule();
        areaWebModule.setRegionAccountId(regionAccountId);
        List<AreaWebModule> areaWebModuleList = areaWebModuleService.select(areaWebModule);
        List<String> stringList=new ArrayList<>();

        for (AreaWebModule areaWebModule1:areaWebModuleList){
            stringList.add(areaWebModule1.getWebModuleNumber());
        }
        return Response.ok(stringList);

    }

    /**
     * 查询开户地区所显示模块
     *
     * @param regionAccountId
     * @return
     */
    @OpenMethod(key = "boss.AreaWebModule.selectWebModuleByRegionAccountId",
            paramNames = {"regionAccountId"},
            httpMethods = RequestMethod.GET)
    public Response<List> selectWebModuleByRegionAccountId(Long regionAccountId) {
        AreaWebModule areaWebModule = new AreaWebModule();
        areaWebModule.setRegionAccountId(regionAccountId);
        List<AreaWebModule> areaWebModuleList = areaWebModuleService.select(areaWebModule);


        return Response.ok(areaWebModuleList);

    }


}
